<?php
/*
 * Copyright (c) 2022.  Liuhongbin All rights reserved.
 * @domain http://dkyx.cc
 * @author liuhongbin <514660094@qq.com>
 */

namespace hiduoke\services\cloud;

use app\common\model\platform\Platform;
use app\common\model\platform\PlatformAddon;
use app\common\model\platform\PlatformApp;
use app\common\model\system\Config;

class DiyCloud extends BaseCloud
{
    protected $basePath = 'diy/';
    protected $baseData = [];
    /**
     * @var PlatformAddon|array|mixed|\think\Model|null
     */
    private $platformAddon;
    /**
     * @var array|mixed
     */
    private $cloud;

    public function __construct($plaid = '', $addon_name = '')
    {
        $plaid = $plaid ?: get_plaid();
        $appid = get_appid();
        // 判断平台类型
        $platform = (new Platform())->where(['plaid' => $plaid])->find();
        $addon_name = $addon_name ?: get_addon();
        if ($platform['type'] == 2 && $appid) {
            $this->platformAddon = (new PlatformApp())->where(['plaid' => $plaid, 'addon_name' => $addon_name, 'id' => $appid])->find();
        } else {
            $this->platformAddon = (new PlatformAddon())->where(['plaid' => $plaid, 'addon_name' => $addon_name])->find();
        }
        parent::__construct();
    }

    public function initialize()
    {
        $this->cloud = Config::get("cloud");
        $this->cloud = array(
            'site_id' => '17',
            'token'   => '4491bb9e7d18d561450f083e4261fbc8',
            'domain'  => 'demo.dkyx.cc'
        );
        $this->baseData = array(
            'site_id'     => $this->cloud['site_id'],
            'token'       => $this->cloud['token'],
            'domain'      => $this->cloud['domain'] ?: request()->server("SERVER_NAME"),
            'addon_token' => $this->platformAddon['token'] ?? '',
            'addon_name'  => $this->platformAddon['addon_name'] ?? '',
        );
    }

    public function getList($page_name, $page, $search = [], $size = 12)
    {
        return $this->post('getList', [], ['page' => $page, 'size' => $size, 'page_name' => $page_name, 'search' => $search]);
    }


    public function add($data)
    {
        $res = $this->post('add', [], ['data' => $data]);
        return $res;
    }

    public function down($id = 0)
    {
        $res = $this->post('down', ['id' => $id]);
        return $res;
    }

    public function del($id = 0)
    {
        $res = $this->post('del', ['id' => $id]);
        return $res;
    }

    public function check($data)
    {
        $res = $this->post('check', ['data' => $data]);
        return $res;
    }

    public function getDiy($id)
    {
        $res = $this->post('get', ['id' => $id]);
        return $res;
    }

    public function checkDiyData($diy_data)
    {
        $len = strlen(json_encode($diy_data)) ?? 0;
        if ($len <= 0 || $len > 512000) {
            return false;
        } else {
            return true;
        }
    }

    public function getDiyDataSize($diy_data)
    {
        $len = strlen(json_encode($diy_data)) ?? 0;
        return $this->getSize($len);
    }

    public function getSize($filesize)
    {
        if ($filesize >= 1073741824) {

            $filesize = round($filesize / 1073741824 * 100) / 100 . ' GB';

        } elseif ($filesize >= 1048576) {

            $filesize = round($filesize / 1048576 * 100) / 100 . ' MB';

        } elseif ($filesize >= 1024) {

            $filesize = round($filesize / 1024 * 100) / 100 . ' KB';

        } else {
            $filesize = $filesize . ' 字节';
        }
        return $filesize;
    }
}
